﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Data.Odbc;
using System.IO;
using MySql.Data.MySqlClient;
namespace MetaSearcher
{
    class MainProcess
    {

        public static void Main(String[] args)
        {
            Program getPage = new Program();
            OneTwoTrip one = new OneTwoTrip();
            DBManipulation meta = new DBManipulation();
            Ozon ozzy = new Ozon();
            String Connection = "Database=meta;Data Source=188.35.9.179;User Id=Oleg;Password=nanah;";
            MySqlConnection metacon = new MySqlConnection(Connection);
            Console.WriteLine("METASEARCHER STARTS WORKING: " + DateTime.Now);
            while (true)
            {
                Console.WriteLine("[" + DateTime.Now + " ] :");
                Console.WriteLine("     In progress: checking table request...");
                DateTime current_day = DateTime.Today;
                RequestPattern[] req = meta.selectFromRequest(Connection, "where status=0");
                if (req.Length > 0)
                {
                    Console.WriteLine("         Unchecked requests found : " + (req.Length) + ".");
                    for (int i = 0; i < req.Length; i++)
                    {
                        Console.WriteLine("         In progress: forming queries by request #" + req[i].id_request + "...");
                        meta.formQueryByRequest(Connection, req[i].id_request, "normal");
                        Console.WriteLine("         Done: forming queries by request #" + req[i].id_request + ".");
                    }
                }
                else
                {
                    Console.WriteLine("         No unchecked requests found.");
                }
                Console.WriteLine("     Done: cheking table request.");
                Console.WriteLine("     In progress: checking table query...");
                QueryPattern[] que = meta.selectFromQuery(Connection, "where date_query=\"" + current_day.Year + "-" + current_day.Month + "-" + current_day.Day + "\"");
                if (que.Length > 0)
                {

                    Console.WriteLine("         Unchecked current day queries found: " + (que.Length));
                    for (int i = 0; i < que.Length; i++)
                    {
                        Console.WriteLine("             In process: working on query #" + que[i].id_query + "...");
                        if (que[i].resource.Equals("iknow.travel"))
                        {
                            Console.WriteLine("                 " + que[i].city_from + " to " + que[i].city_to + " for " + que[i].date_browse + ". Waiting for response...");
                            DateTime date = que[i].date_browse;
                            String dateFl = date.Year + "-" + date.Month + "-" + date.Day + " T00:00:00.000Z";
                            String result = getPage.getDataFromIknowTravel(dateFl, que[i].city_from.ToString(), que[i].city_to.ToString(), "economy");
                            getPage.IKnowParse(result, meta, Connection);

                            result = getPage.getDataFromIknowTravel(dateFl, que[i].city_from.ToString(), que[i].city_to.ToString(), "business");
                            getPage.IKnowParse(result, meta, Connection);

                        }
                        if (que[i].resource.Equals("OneTwoTrip"))
                        {
                            Console.WriteLine("                     " + que[i].city_from + " to " + que[i].city_to + " for " + que[i].date_browse + ". Waiting for response...");
                            MetaSearcher.OneTwoTrip.onetwotripParse(one.GetResp_12trip(OneTwoTrip.parDate(que[i].date_browse.ToString()), que[i].city_from, que[i].city_to), meta, Connection, que[i].city_from, que[i].city_to, que[i].date_browse.ToString());
                        }
                        if (que[i].resource.Equals("Euroavia"))
                        {

                            Stream stream = one.GetResp_Euroavia(que[i].date_browse, que[i].city_from, que[i].city_to); //получаем первый html поток с начальной страницы
                            string next = one.Euro(stream, que[i].date_browse, Connection, meta, que[i].city_from, que[i].city_to); //парсим и получаем ссылку на следующую

                            while (next[0] != 'h') //последняя ненужная ссылка начинается на h - делаем проверку на первый символ ( нужная начинается на n )
                            {
                                stream = one.GetNextRest_Euroavia(next); // получаем инфу по новой ссылке
                                next = one.Euro(stream, que[i].date_browse, Connection, meta, que[i].city_from, que[i].city_to); //парсим её
                            }
                        }
                        if (que[i].resource.Equals("Ozon"))
                        {
                            Console.WriteLine("                         Запрашиваю перелета из " + que[i].city_from + " в " + que[i].city_to + " на " + que[i].date_browse + ". Жду ответ...");
                            DateTime date = que[i].date_browse;
                            String dateFl = date.Year + "-" + date.Month + "-" + date.Day;
                            string result = ozzy.GetOzoneData(que[i].city_from.ToString(), que[i].city_to.ToString(), dateFl, "ECONOMY");
                            ozzy.OZONParse(result, meta, Connection, que[i].city_from, que[i].city_to);

                            result = ozzy.GetOzoneData(que[i].city_from.ToString(), que[i].city_to.ToString(), dateFl, "BUSINESS");
                            ozzy.OZONParse(result, meta, Connection, que[i].city_from, que[i].city_to);


                            Console.WriteLine("                         Парсинг " + i + " завершен.");
                        }
                        meta.deleteFromQuery(Connection, que[i].id_query);
                        Console.WriteLine("             Done: working on query #" + que[i].id_query + "...");
                    }
                }
                else
                {
                    Console.WriteLine("         No current day queries found.");
                }
                Console.WriteLine("     Done: checking table query.");
                meta.checkStatus_setStatus2(Connection);

                Thread.Sleep(600000);
            }

        }
    }
}
